SHELL = /bin/sh
CPP = g++
CPP_FLAGS = -std=c++0x -fPIC -D_ICECODE_UTILS
AR_FLAGS =
PROJ_NAME = icecode-utils
LIB_NAME = lib$(PROJ_NAME).a
OBJECTS = args.o debug.o utils.o passwd.o
DESTDIR = /usr
includedir = /include
libdir = /lib
projdir = /icecode/utils

.PHONY: install uninstall clean

$(LIB_NAME): $(OBJECTS)
	$(AR) rcs $(AR_FLAGS) $(LIB_NAME) $(OBJECTS)

$(OBJECTS): %.o : %.cpp
	$(CPP) -c -o $@ $(CPP_FLAGS) $<

install:
	@if [[ ! -f $(LIB_NAME) ]]; then echo "Compile the library first."; exit 1; fi
	@install -m 755 -d $(DESTDIR)$(includedir)$(projdir)
	@install -m 644 $(LIB_NAME) $(DESTDIR)$(libdir)
	@for hpp in *.hpp; do install -m 444 $$hpp $(DESTDIR)$(includedir)$(projdir); done

uninstall:
#Remove headers
	if [[ ! -d $(DESTDIR)$(includedir)$(projdir) ]]; then $(info Headers not found.); else \
	 for f in *.hpp; do echo Removing $$f...; $(RM) $(DESTDIR)$(includedir)$(projdir)/$$f; done \
	 rmdir -p $(DESTDIR)$(includedir)$(projdir)
	fi

#Remove library
	$(RM) $(DESTDIR)$(libdir)/$(LIB_NAME)

clean:
	$(RM) $(OBJECTS) $(LIB_NAME)
